<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');
App::uses('CakeTime', 'Utility');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
		
	public $components = array('Auth' => array(
		'loginRedirect' => array('controller' => 'home', 'action' => 'index')
		)
	,'Session','Email');
	public $helpers = array('Html', 'Form','Session','Time','Status','Date');
	var $menu = array();
	
	public function beforeRender () {
		$this->menu['home'] = 1;
		$this->menu['system'] = $this->isAllow('system');
		$this->menu['duyethoso'] = $this->isAllow('duyethoso');
		$this->menu['tiepnhan'] = $this->isAllow('tiepnhan');
		$this->menu['phanxuly'] = $this->isAllow('phanxuly');
		$this->menu['xuly'] = $this->isAllow('xuly');
		$this->menu['kiemduyet'] = $this->isAllow('kiemduyet');
		$this->menu['kyduyet'] = $this->isAllow('kyduyet');
		$this->menu['traketqua'] = $this->isAllow('traketqua');
		$this->menu['baocao'] = $this->isAllow('baocao');
		$this->menu['tracuu'] = $this->isAllow('tracuu');
		
		// debug($this->menu);
		$this->set("menu", $this->menu);
		
		$this->set("selectedMenu", $this->request->params['controller']);
		$this->set("app",'');
		$this->set("user",$this->Auth->user());
	}
	
	public function isAllow($request = null) {
		$request = empty($request) ? $this->request->controller: $request;
		// debug($request);
		$permission = $this->Session->read("permission");
		// debug($permission);
		$user = $this->Auth->user();
//		debug($user);
		
		return !empty($permission[$user['username']][$request]);
	}
	
	public function convertStrDate($date) {
		if (empty($date)) return "";
		list($m,$d,$y) = explode('/', $date);
		return date('Y-m-d', mktime(0, 0, 01, $d, $m, $y));
	}
	/**
	 * compare days between two dates
	 */
	public function compareDate($date1, $date2) {
		$interval = $date1->diff($date2);
		return ($date1 > $date2)? (0 - $interval->days): $interval->days;
	}
	
	public function isEdit($hoso) {
		$uneditStatus = array(
			$this->Status->HS_DA_KY,
			$this->Status->HS_CHO_TRA_KQ,
			$this->Status->HS_DA_LUU_TRU,
			$this->Status->HS_DA_TRA_KQ,
			$this->Status->HS_TRE_HEN,
			$this->Status->HS_KHONG_DEN_NHAN_KQ,
			$this->Status->HS_KHONG_DAT_CHO_TRA_KQ
		);
		return in_array($hoso['Hoso']['trang_thai'], $uneditStatus);
	}
}
